﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Manager
{
    public partial class frmAddCV : Form
    {
        SQL sql = new SQL();
        string strMNV;

        public frmAddCV()
        {
            InitializeComponent();
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Ban co muon insert cong viec cho nhan vien ??", "Info", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {               
                SqlParameter[] sqlPara = new SqlParameter[4];
                sqlPara[0] = new SqlParameter("@MNV", strMNV);
                sqlPara[1] = new SqlParameter("@MCV", cbMCV.Text);
                sqlPara[2] = new SqlParameter("@DATETG", dateTGCV.Value.Date.ToString("MM/dd/yyyy"));
                sqlPara[3] = new SqlParameter("@DATEKT", dateKTCV.Value.Date.ToString("MM/dd/yyyy"));
                string strQuery = "sql_insert_CT_NhanVien_CongViec";
                sql.execute_Query(strQuery, sqlPara);
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cbMCV_SelectedIndexChanged(object sender, EventArgs e)
        {
            getNameCV(cbMCV.Text);
        }
        public void getNameCV(string strMCV)
        {
            try
            {                
                SqlParameter sqlPara = new SqlParameter("@MCV", strMCV);
                string strQuery="sql_NAMECV_where_MCV";
                DataTable dt = sql.getDataTable(strQuery, sqlPara);
                tbnameCV.Text = dt.Rows[0][0].ToString();
            }
            catch
            {
                tbnameCV.Text = "";
            }
        }

        private void cbMCT_SelectedIndexChanged(object sender, EventArgs e)
        {
            getMCV(cbMCT.Text);
            getNameCT(cbMCT.Text);
        }
        

        public void getNameCT(string strMCT)
        {
            try
            {               
                SqlParameter sqlPara = new SqlParameter("@MCT", strMCT);
                string strQuery = "sql_NAMECT_where_MCT";
                DataTable dt = sql.getDataTable(strQuery, sqlPara);
                tbNameCT.Text = dt.Rows[0][0].ToString();
            }
            catch
            {
                tbNameCT.Text = "";
            }
        }
        public void getMCV(string strMCT)
        {           
            SqlParameter sqlPara = new SqlParameter("@MCT", strMCT);
            string strQuery = "sql_MCV_where_MCT";
            DataTable dt = sql.getDataTable(strQuery, sqlPara);
            cbMCV.DataSource = dt;
            cbMCV.ValueMember = "MCV";
            cbMCV.Text = "";
        }
        public void getMNV(string strMNV)
        {
            this.strMNV = strMNV;
        }
        public void getValue()
        {
            SqlParameter sqlPara = new SqlParameter("@MNV", strMNV);
            string strQuery = "sql_select_NhanVien";
            DataTable dt = sql.getDataTable(strQuery, sqlPara);
            if (dt == null)
            {
                MessageBox.Show("Please choose Nhan vien ?", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.Close();
                return;
            }

            tbMNV.Text = dt.Rows[0][0].ToString();
            cbMPB.Text = dt.Rows[0][1].ToString();
            tbnameNV.Text = dt.Rows[0][2].ToString();
            tbAddress.Text = dt.Rows[0][3].ToString();
            dateIntoCompany.Value = Convert.ToDateTime(dt.Rows[0][4].ToString());
            dateBirtday.Value = Convert.ToDateTime(dt.Rows[0][5].ToString());

            sqlPara = new SqlParameter("@MNV", tbMNV.Text);
            strQuery = "sql_MCT_where_MNV";
            dt = sql.getDataTable(strQuery, sqlPara);
            if (dt == null)
            {
                MessageBox.Show("Please choose Nhan vien ?", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.Close();
                return;
            }
            cbMCT.DataSource = dt;
            cbMCT.ValueMember = "MCT";
            cbMCT.Text = "";
            //get Name CT
            getNameCT(cbMCT.Text);
        }

        private void frmAddCV_Load(object sender, EventArgs e)
        {
            getValue();            
        }

    }
}
